<?php
namespace Controller;

use Hoa\Registry\Registry;
use Library\Log;
use Library\View;
use Linice\Pagination\Pagination;
use Logic\CnfLogic;
use Model\Cnf;
use Service\CnfService;
use Validator\CnfValidator;


/**
 * CnfController
 * 配置
 */
class CnfController extends BaseController {
	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct();
	}


	/**
	 * test
	 */
	public function test() {
		echo __METHOD__;
	}


	/**
	 * 配置列表
	 */
	public function cnf() {
		//定义变量
		$today = date('Y-m-d');
		$one_month_ago = date('Y-m-d', strtotime('-1 month'));

		//获取参数
		$type = I('get.type');
		$k = I('get.k');
		$v = I('get.v');
		$page = I('get.page', 1, ['trim', 'intval']);

		//定义变量
		$conf = Registry::get('conf');
		$pagesize = $conf['base']['pagesize'];
		$param = [
			'type' => $type,
			'k' => $k,
			'v' => $v,
		];

		//搜索配置
		$cnf_result = CnfService::searchCnf($param, $page, $pagesize);
		$cnf_cnt = $cnf_result['cnt'];

		//分页
// 		Log::debug('page: ' . $page);
// 		Log::debug('pagesize: ' . $pagesize);
// 		Log::debug('cnf_cnt: ' . $cnf_cnt);
		$pagination = new Pagination;
		$pagination->setCurrent($page);
		$pagination->setRPP($pagesize);
		$pagination->setTotal($cnf_cnt);
		$pager = $pagination->parse();

		//Success
		$data = [];
		$data['param'] = json_encode($param);
		$data['cnf_list'] = $cnf_result['list'];
		$data['cnf_pager'] = $pager;
		$data['cnf_page'] = $cnf_cnt ? $page : 0;
		$data['cnf_total_page'] = ceil($cnf_cnt / $pagesize);
		$data['cnf_cnt'] = $cnf_cnt;
		View::display('Cnf/cnf', $data);
	}


	/**
	 * 新增配置
	 */
	public function addCnf() {
		View::display('Cnf/addCnf');
	}


	/**
	 * 处理新增配置
	 */
	public function doAddCnf() {
		//定义变量
		$conf = Registry::get('conf');
		$ret = $conf['status'][200];

		//获取参数
		$type = I('post.type');
		$k = I('post.k');
		$v = I('post.v');
		$idx = I('post.idx');
		$remark = I('post.remark');
		$enable = I('post.enable');

		//验证参数
		//验证类型
		$validateTypeResult = CnfValidator::validateType($type);
		if (true !== $validateTypeResult) {
			exit(json_encode($validateTypeResult));
		}

		//验证键
		$validateKResult = CnfValidator::validateK($k);
		if (true !== $validateKResult) {
			exit(json_encode($validateKResult));
		}

		//处理新增配置
		$data = array(
			'type' => $type,
			'k' => $k,
			'v' => $v,
			'idx' => $idx,
			'remark' => $remark,
			'enable' => $enable,
		);

		//查询是否已存在相同的类型和键的组合
		$same_cnf = Cnf::getCnfByTypeAndK($type, $k);
		if ($same_cnf) {
			$ret = $conf['status'][410301];
			exit(json_encode($ret));
		}
		$result = CnfService::addCnf($data);
		if (!$result) {
			$ret = $conf['status'][510101];	 //新增保存失败
			exit(json_encode($ret));
		}
		exit(json_encode($ret));
	}


	/**
	 * 批量新增配置
	 */
	public function batchAddCnf() {
		View::display('Cnf/batchAddCnf');
	}


	/**
	 * 处理批量新增配置
	 */
	public function doBatchAddCnf() {
		//定义变量
		$conf = Registry::get('conf');
		$ret = $conf['status'][200];
		$cnf_list = [];

		//获取参数
		$type_list = I('post.type');
		$k_list = I('post.k');
		$v_list = I('post.v');
		$idx_list = I('post.idx');
		$remark_list = I('post.remark');
		$enable_list = I('post.enable');
		//Log::debug('type_list: ' . var_export($type_list, true));

		for ($i = 0; $i < count($type_list); $i++) {
			$cnf = ['type' => $type_list[$i],
				'k' => $k_list[$i],
				'v' => $v_list[$i],
				'idx' => $idx_list[$i],
				'remark' => $remark_list[$i],
				'enable' => $enable_list[$i],
			];
			$cnf_list[] = $cnf;
		}

		//批量添加
		$result = CnfService::batchAddCnf($cnf_list);
		if ($result !== true) {
			$ret = $conf['status'][410301];
			$ret['i'] = $result;
			exit(json_encode($ret));
		}
		exit(json_encode($ret));
	}


	/**
	 * 处理删除配置
	 */
	public function doDelCnf() {
		//定义变量
		$conf = Registry::get('conf');
		$ret = $conf['status'][200];

		//获取参数
		$id = I('post.id');

		//删除配置
		CnfService::delCnf($id);
		exit(json_encode($ret));
	}


	/**
	 * 处理批量删除配置
	 */
	public function doBatchDelCnf() {
		//定义变量
		$conf = Registry::get('conf');
		$ret = $conf['status'][200];

		//获取参数
		$id_list = I('post.id_list');

		//批量删除
		$result = CnfService::batchDelCnf($id_list);
		if (!$result) {
			$ret = $conf['status'][510102];
			exit(json_encode($ret));
		}
		exit(json_encode($ret));
	}


	/**
	 * 修改配置
	 */
	public function editCnf() {
		//获取参数
		$id = I('get.id');

		//查询配置
		$cnf = Cnf::get($id);

		//Success
		$data = [];
		$data['cnf'] = json_encode($cnf);
		View::display('Cnf/editCnf', $data);
	}


	/**
	 * 处理修改配置
	 */
	public function doEditCnf() {
		//定义变量
		$conf = Registry::get('conf');
		$ret = $conf['status'][200];

		//获取参数
		$id = I('post.id');
		$type = I('post.type');
		$k = I('post.k');
		$v = I('post.v');
		$idx = I('post.idx');
		$remark = I('post.remark');
		$enable = I('post.enable');

		//验证参数
		//验证类型
		$validateTypeResult = CnfValidator::validateType($type);
		if (true !== $validateTypeResult) {
			exit(json_encode($validateTypeResult));
		}

		//验证键
		$validateKResult = CnfValidator::validateK($k);
		if (true !== $validateKResult) {
			exit(json_encode($validateKResult));
		}

		//修改配置
		$cnf_data = [
			'type' => $type,
			'k' => $k,
			'v' => $v,
			'idx' => $idx,
			'remark' => $remark,
			'enable' => $enable,
		];

		//检查是否有相同的类型和键的组合
		$same_cnf = Cnf::findSameEditCnf($type, $k, $id);
		if ($same_cnf) {
			$ret = $conf['status'][410301];   //类型，键的组合已存在
			exit(json_encode($ret));
		}
		$result = CnfService::editCnf($id, $cnf_data);
		if (!$result) {
			$ret = C('status.510101');
			exit(json_encode($ret));
		}
		exit(json_encode($ret));
	}


	/**
	 * 批量修改配置
	 */
	public function batchEditCnf() {
		//获取参数
		$id_list = I('get.id_list');

		//查询配置列表
		$cnf_list = Cnf::getList($id_list);

		//Success
		$data = [];
		$data['cnf_list'] = $cnf_list;
		View::display('Cnf/batchEditCnf', $data);
	}


	/**
	 * 处理批量修改配置
	 */
	public function doBatchEditCnf() {
		//定义变量
		$conf = Registry::get('conf');
		$ret = $conf['status'][200];
		$cnf_list = [];

		//获取参数
		$id_list = I('post.id');
		$type_list = I('post.type');
		$k_list = I('post.k');
		$v_list = I('post.v');
		$idx_list = I('post.idx');
		$remark_list = I('post.remark');
		$enable_list = I('post.enable');
		//Log::debug('type_list: ' . var_export($type_list, true));

		for ($i = 0; $i < count($id_list); $i++) {
			$cnf = ['id' => $id_list[$i],
					'type' => $type_list[$i],
					'k' => $k_list[$i],
					'v' => $v_list[$i],
					'idx' => $idx_list[$i],
					'remark' => $remark_list[$i],
					'enable' => $enable_list[$i],
			];
			$cnf_list[] = $cnf;
		}

		//批量添加
		$result = CnfService::batchEditCnf($cnf_list);
		if ($result !== true) {
			$ret = $conf['status'][410301];
			$ret['id'] = $result;
			exit(json_encode($ret));
		}
		exit(json_encode($ret));
	}

}